package vip.liux.front.adapter.web.account;

import org.apache.commons.lang.math.RandomUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserCache;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/api/account/user")
public class UserController {

    private final UserCache userCache;

    public UserController(UserCache userCache) {
        this.userCache = userCache;
    }

    @GetMapping("/info")
    public Map<String, Object> info(Authentication authentication) {
        return Map.of("userInfo", userCache.getUserFromCache(authentication.getName()));
    }

    @GetMapping("/assets")
    public Map<String, Object> assets() {
        return Map.of("assets", Map.of(
                "balance", RandomUtils.nextInt(100),
                "points", RandomUtils.nextInt(100),
                "coupon", RandomUtils.nextInt(100))
        );
    }

    @RestController
    @RequestMapping("/api/order")
    public static class OrderController {
        @GetMapping("/todo-counts")
        public Map<String, Object> todoCounts() {
            return Map.of("counts", Map.of(
                    "payment", 0,
                    "delivery", 0,
                    "received", 0,
                    "refund", 0)
            );
        }
    }
}
